<script setup>
import { ref } from 'vue'
import axios from 'axios'

const emit = defineEmits(['login'])

const account = ref('')
const password = ref('')

const goToLogin = () => {
  emit('login') // 触发父组件的login事件
}

const handleRegister = () => {
  axios.post('https://hmajax.itheima.net/api/register', {
    account: account.value,
    password: password.value
  }).then(res => {
    console.log('注册成功', res)
    goToLogin() // 注册成功后自动跳转到登录
  }).catch(err => {
    console.error('注册失败', err)
  })
}
</script>

<template>
  <div class="register-box">
    <el-col :span="10" class="registerForm">
    <h2>注册</h2>
    <el-row>
      <input class="inputBox" type="text" placeholder="请输入账号" v-model="account">
    </el-row>

    <el-row>
      <input class="inputBox" type="password" placeholder="请输入密码" v-model="password">
    </el-row>

    <el-row>
      <button class="inputregister" @click="handleRegister">注册</button>
    </el-row>
    <p>没有账号？<span @click="goToLogin" :style="{ color: '#95eb82', cursor: 'pointer' }">立即注册</span></p>
  </el-col>
  </div>
</template>

<style scoped lang="less">
.register-box {
  width: 1000px;
  height: 500px;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  position: relative;
  background-color: rgba(0, 0, 0, 0.2);
  border-radius: 10px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
  padding: 20px;
  background-image: url('@/assets/images/loginBackground.jpg');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  // clip-path: polygon(0 0, 100% 100%, 0 100%); // 裁剪形状 左上角为起点，右下角为终点


  .registerForm {
    right: 0;
    position: absolute;
    top: 50%;
    color: #fff;
    transform: translateY(-50%); // 居中
    width: 700px;
    height: 300px;
    background-color: rgba(0, 0, 0, 0.2);
    border-radius: 10px;
    padding: 20px;


  }

  .inputBox {
    width: 100%;
    margin: 10px auto;
    padding: 10px;
    border-radius: 5px;
    border: none;
    background-color: #fff;
    color: #000;
    font-size: 16px;
    box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);
    transition: all 0.3s ease; // 过渡效果

    &:hover {
      background-color: rgba(255, 0.8);
      cursor: pointer;
    }
  }

  .inputregister {
    margin: 10px auto;
    width: 100%;
    height: 40px;
    background-color: #95eb82;
    text-align: center;

  }


}
</style>